package com.itheima.Controller;

import com.itheima.pojo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

//会话跟踪
@RestController
public class SessionController {
    @GetMapping("/c1")
    public Result cokkie1(HttpServletResponse response){
        //模拟获取账号密码..登录成功23
        // 设置Cookie会话信息
        response.addCookie(new Cookie("usename1","李小志"));
        response.addCookie(new Cookie("usename2","王小强"));
        return Result.success();

    }



//    Cookie会话技术33*2、获取Cookie中用户的登录信息（身份信息）
    @GetMapping("/c2")
    public Result cookie2(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if (name.equals("usename1")){
                System.out.println("username="+cookie.getValue());
            }
        }
        return Result.success();

    }
//    seesion绘画
    @GetMapping("/s1")
    public  Result session1(HttpSession seesion){
        System.out.println("会话标识："+ seesion.hashCode());

        int count = new Random().nextInt();
        String username="tom"+count;
        seesion.setAttribute("looo","olll"+username);
        return Result.success();

    }

    @GetMapping("/s2")
    public Result sseion2(HttpServletRequest request){
        HttpSession session= request.getSession();
        Object LOGINuSER=session.getAttribute("loginUser");
        System.out.println(session.hashCode()+":"+LOGINuSER);
        return Result.success();
    }





}
